package com.it.sun.easychat.common.context;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.*;

/**
 * @author sunmannian
 * @ClassName com.it.sun.easychat.common.context.SessionListener
 * @Date 2020/6/2 19:54
 * @Version V1.0
 * Description  <请输入一句话描述该类功能>
 * Copyright (C) 北京中电兴发科技有限公司  since 2020/6/2
 */
@Component
@Slf4j
public class SessionListener implements HttpSessionListener, HttpSessionAttributeListener {

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        Current current = new Current();
        HttpSession session = se.getSession();
//        current.setSession(session);
        session.setAttribute(Current.CURRENT_NAME, current);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        RequestContextHolder.resetRequestAttributes();
        log.info("session destroyed ");
    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent se) {
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent se) {
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent se) {
    }
}
